var zkd = {
    setBtnStatus:function () {
        var row = $('#grid_zksq').datagrid('getSelected');
        if (row) {
            if (row.zt == "ybc") {
                $("#btn_zksq_update").show();
                $("#btn_zksq_del").show();
                $("#btn_zksq_tj").show();
                $("#btn_zksq_sh").hide();
                $("#btn_zksq_zk").hide();
                $("#btn_zksq_ps").hide();
                $("#btn_zksq_zr").hide();
            } else if (row.zt == "ytj") {
                $("#btn_zksq_update").hide();
                $("#btn_zksq_sh").show();
                $("#btn_zksq_del").hide();
                $("#btn_zksq_tj").hide();
                $("#btn_zksq_zk").hide();
                $("#btn_zksq_ps").hide();
                $("#btn_zksq_zr").hide();
            } else if (row.zt == "ysp") {
                $("#btn_zksq_update").hide();
                $("#btn_zksq_del").hide();
                $("#btn_zksq_tj").hide();
                $("#btn_zksq_sh").hide();
                $("#btn_zksq_zr").hide();
                if (row.xyps == true && row.yps == false) {
                    $("#btn_zksq_ps").show();
                    $("#btn_zksq_zk").hide();
                } else {
                    $("#btn_zksq_zk").show();
                    $("#btn_zksq_ps").hide();
                }
            } else if (row.zt == "yzc") {
                $("#btn_zksq_del").hide();
                $("#btn_zksq_update").hide();
                $("#btn_zksq_sh").hide();
                $("#btn_zksq_zk").hide();
                $("#btn_zksq_tj").hide();
                $("#btn_zksq_ps").hide();
                $("#btn_zksq_zr").show();
            } else if (row.zt == "yzr") {
                $("#btn_zksq_del").hide();
                $("#btn_zksq_update").hide();
                $("#btn_zksq_sh").hide();
                $("#btn_zksq_zk").hide();
                $("#btn_zksq_tj").hide();
                $("#btn_zksq_ps").hide();
                $("#btn_zksq_zr").hide();
            }
        } else {
            $("#btn_zksq_del").hide();
            $("#btn_zksq_update").hide();
            $("#btn_zksq_sh").hide();
            $("#btn_zksq_zk").hide();
            $("#btn_zksq_tj").hide();
            $("#btn_zksq_ps").hide();
            $("#btn_zksq_zr").hide();
        }
    },
    showDetail:function (id) {
        zkd.alwayshowDetail(id);
    },
    alwayshowDetail:function (id) {
        $('#layout_zksq').layout('remove', 'center');
        $('#layout_zksq').layout('add', {
            region:'center',
            split:true,
            href:$.webPath() + '/kcgl/zksq/zksq_detail?zkdId=' + id
        });
    }
}


$(function () {
    //表格初始加载
    $('#grid_zksq').datagrid({
        url:$.webPath() + '/kcgl/zksq/zkdList',
        toolbar:"#toolbar_zksq",
        fitColumns:true,
        singleSelect:true,
        pageNumber:1,
        pagination:true,
        fit:true,
        rownumbers:true,
        striped:true,
        onLoadSuccess:function () {
            $(this).datagrid('selectRow', 0);
            zkd.setBtnStatus();

            $(".datagrid-htable .datagrid-cell").css('text-align', 'center');
        },
        onClickRow:function () {
            zkd.setBtnStatus();
        },
        onSelect:function (rowIndex, rowData) {
            if (rowData) {
                zkd.setBtnStatus();
            }
        },
        columns:[
            [
                {field:'zkdh', title:'转库单号', align:'center', width:40, formatter:zkshCell},
                {field:'sqgs_jc', title:'申请公司', align:'center', width:40},
                {field:'zcck_mc', title:'转出仓库', align:'center', width:60},
                {field:'zrck_mc', title:'转入仓库', align:'center', width:60},
                {field:'zkrq', title:'转出日期', align:'center', width:40},
                {field:'zrrq', title:'转入日期', align:'center', width:40},
                {field:'yps', title:'配送状态', width:40, align:'center',
                    formatter:function (value, rowData, rowIndex) {
                        if (rowData.xyps == false) {
                            return '自提';
                        } else if (rowData.xyps == true && value == false) {
                            return '<a style="color:red">未配送</a>';
                        } else if (rowData.xyps == true && value == true) {
                            return '已配送';
                        }
                    }},
                {field:'type', title:'类型', align:'center', width:20,
                    formatter:function (value, row, index) {
                        if (value == 'bgzk') {
                            return '保供转库';
                        } else if (value == "zczk") {
                            return '正常转库';
                        }
                    }},
                {field:'zt', title:'状态', align:'center', width:20,
                    formatter:function (value, row, index) {
                        if (value == 'ybc') {
                            return '已保存';
                        } else if (value == "ytj") {
                            return '已提交';
                        } else if (value == "ysp") {
                            return '已审批';
                        } else if (value == "yzc") {
                            return '已转出';
                        } else if (value == "yzr") {
                            return '已转入';
                        }
                    }}
            ]
        ]
    });
    //**********按钮触发事件**********
    //新增按钮
    $("#btn_zksq_add").click(function () {
        zksq_add('新增转库单', 'add_zksq');
    });
    //打印
    $("#btn_zksq_print").click(function () {
        zksq_print('转库审核打印', 'print_zksq');
    });
    //配送
    $("#btn_zksq_ps").click(function () {
        zkd_ps('新增转库配送单', 'add_zkd_ps');
    });
    //修改
    $("#btn_zksq_update").click(function () {
        zksq_update('修改转库单', 'update_zksq');
    });
    //删除
    $("#btn_zksq_del").click(function () {
        zdsq_del();
    })
    //提交
    $("#btn_zksq_tj").click(function () {
        zksq_commit();
    });
    //审批
    $("#btn_zksq_sh").click(function () {
        zksq_sh();
    });
    //转出
    $("#btn_zksq_zk").click(function () {
        zksq_zk('转库单', 'zk_zksq');
    });
    //转入
    $("#btn_zksq_zr").click(function () {
        zksq_zr('转库单', 'zk_zksq_zr');
    });
    //查询按钮
    $("#btn_zksq_search").click(function () {
        zksq_search();
    });
});

//新增转库配送单
function zkd_ps(title, action) {
    var record = $("#grid_zksq").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要配送的转库!", "warning");
        return;
    }
    var href = $.webPath() + "/kcgl/zksq/zksq_ps_content?zkdId=" + record.id;
    var actionUrl = $.webPath() + "/kcgl/zksq/add_zkd_ps";
    zkd_psShowDialog(title, href, actionUrl);
}

//弹出转库配送的窗口
function zkd_psShowDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:750,
        height:430,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    $(win).mask();
                    $('#form_zkd_ps').form('submit', {
                        url:actionUrl,
                        onSubmit:function (param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                $(win).mask('hide');
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            $(win).mask('hide');
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_zksq').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

function zksq_print(title, action) {
    var record = $("#grid_zksq").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要打印的记录!", "warning");
        return;
    }
    var href = $.webPath() + "/kcgl/zksq/zksq_content_print?zkdId=" + record.id;
    var actionUrl = $.webPath() + "/kcgl/zksq/" + action;
    zksq_showPrintDialog(title, href, actionUrl);
}

function zksq_showPrintDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:1024,
        height:500,
        cache:false,
        url:href,
        modal:true,
        maximizable:true
    });
}


//新增转库单
function zksq_add(title, action) {
    var href = $.webPath() + "/kcgl/zksq/zksq_content";
    var actionUrl = $.webPath() + "/kcgl/zksq/add_zksq";
    zksq_showDialog(title, href, actionUrl);
}

//修改转库单
function zksq_update(title, action) {
    var record = $("#grid_zksq").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要修改的记录！", "warning");
        return;
    }
    var href = $.webPath() + "/kcgl/zksq/zksq_content?zkdId=" + record.id;
    var actionUrl = $.webPath() + "/kcgl/zksq/" + action;
    zksq_showDialog(title, href, actionUrl);
}

//转出
function zksq_zk(title, action) {
    var record = $("#grid_zksq").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要办理的转库单!", "warning");
        return;
    }
    var href = $.webPath() + "/kcgl/zksq/zksq_zkcontent?zkdId=" + record.id;
    var actionUrl = $.webPath() + "/kcgl/zksq/" + action;
    zksq_zcrDhowDialog(title, href, actionUrl);
}
//转入
function zksq_zr(title, action) {
    var record = $("#grid_zksq").datagrid("getSelected");
    if (record == null) {
        $.messager.alert("提示", "请选择要办理的转库单!", "warning");
        return;
    }
    var href = $.webPath() + "/kcgl/zksq/zksq_zrcontent?zkdId=" + record.id;
    var actionUrl = $.webPath() + "/kcgl/zksq/" + action;
    zksq_zcrDhowDialog(title, href, actionUrl);
}

function zdsq_del() {
    var row = $('#grid_zksq').datagrid('getSelected');
    if (row) {
        $.messager.confirm('确认', '是否确认删除此转库单', function (r) {
            if (r) {
                var index = $('#grid_zksq').datagrid('getRowIndex', row);
                $.post($.webPath() + '/kcgl/zksq/del_zksq', {"zkdId":row.id}, function (data) {
                    var obj = eval('(' + data + ')');
                    if (obj.statusCode == '200')
                        $('#grid_zksq').datagrid('deleteRow', index);
                    else
                        $.messager.alert('提示', obj.message, 'info');
                });
            }
        });
    } else {
        $.messager.alert('提示', "请选择要删除的转库单", 'info');
    }
}

//弹出新增或修改的窗口
function zksq_zcrDhowDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:750,
        height:430,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    $('#zkdActionForm').form('submit', {
                        url:actionUrl,
                        onSubmit:function (param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                return false;
                            }
                            $('#zkd_mx_action_table').edatagrid('acceptChanges');
                            $('#zkd_fy').edatagrid('acceptChanges');
                            var data = $('#zkd_mx_action_table').edatagrid("getData");
                            var fydata = $('#zkd_fy').edatagrid("getData");
                            if (!data.rows.length) {
                                $.messager.alert("提示", "请新增转库单明细!", "waring");
                                $('#zkdActionForm').tabs('select', "转库信息");
                                return false;
                            }
                            var arrayPrefix = 'zkd.zkdmxList';
                            var fyPrefix = 'zkd.zkfyList';
                            var paramString = "{";
                            //物资计划明细
                            for (var i = 0; i < data.rows.length; i++) {
                                {
                                    var objectString = $.toJSONStringPrefix(data.rows[i], arrayPrefix + "[" + i + "].");
                                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                                }

                            }

                            //费用物资计划
                            for (var i = 0; i < fydata.rows.length; i++) {
                                {
                                    var objectString = $.toJSONStringPrefix(fydata.rows[i], fyPrefix + "[" + i + "].");
                                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                                }
                            }
                            if (data.rows && data.rows.length > 0)
                                paramString = paramString.substring(0, paramString.length - 1);
                            paramString += "}";
                            $.extend(param, $.parseJSON(paramString));
                            return isValid;
                        },
                        success:function (data) {
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_zksq').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//弹出新增或修改的窗口
function zksq_showDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:750,
        height:430,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'保存',
                handler:function () {
                    $('#zkdForm').form('submit', {
                        url:actionUrl,
                        onSubmit:function (param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                return false;
                            }
                            $('#zkd_mx').edatagrid('acceptChanges');
                            $('#zkd_fy').edatagrid('acceptChanges');
                            var data = $('#zkd_mx').edatagrid("getData");
                            var fydata = $('#zkd_fy').edatagrid("getData");
                            if (!data.rows.length) {
                                $.messager.alert("提示", "请新增转库单明细!", "waring");
                                $('#zkdForm').tabs('select', "转库信息");
                                return false;
                            }

                            var arrayPrefix = 'zkd.zkdmxList';
                            var fyPrefix = 'zkd.zkfyList';
                            var paramString = "{";
                            //物资计划明细
                            for (var i = 0; i < data.rows.length; i++) {
                                if (data.rows[i].yksl != 0 && data.rows[i].zksl != null && data.rows[i].zksl > data.rows[i].yksl) {
                                    $.messager.alert('提示', '第' + (i + 1) + '行的转库数量大于库存数量', 'info');
                                    return false;
                                }
                                var objectString = $.toJSONStringPrefix(data.rows[i], arrayPrefix + "[" + i + "].");
                                paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                            }

                            //费用物资计划
                            for (var i = 0; i < fydata.rows.length; i++) {
                                {
                                    var objectString = $.toJSONStringPrefix(fydata.rows[i], fyPrefix + "[" + i + "].");
                                    paramString = paramString + objectString.substring(1, objectString.length - 1) + ",";
                                }
                            }
                            if (data.rows && data.rows.length > 0)
                                paramString = paramString.substring(0, paramString.length - 1);
                            paramString += "}";
                            $.extend(param, $.parseJSON(paramString));
                            return isValid;
                        },
                        success:function (data) {
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_zksq').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}

//转库弹出框
function zksq_showActionDialog(title, href, actionUrl) {
    var win = $.window({
        title:title,
        width:750,
        height:430,
        cache:false,
        url:href,
        modal:true,
        buttons:[
            {
                text:'转库',
                handler:function () {
                    $('#zkdActionForm').form('submit', {
                        url:actionUrl,
                        onSubmit:function (param) {
                            var isValid = $(this).form('validate');
                            if (!isValid) {
                                return false;
                            }
                            return isValid;
                        },
                        success:function (data) {
                            var json = $.parseJSON(data);
                            if (json.statusCode == 200) {
                                $(win).window('close');
                                $('#grid_zksq').datagrid('reload');
                                $.messager.show({
                                    title:'提示',
                                    msg:json.message,
                                    timeout:3000,
                                    showType:'slide'
                                });
                            } else
                                $.messager.alert('提示', json.message, 'error');
                        }
                    });
                }
            }
        ]
    });
}
//提交方法
function zksq_commit() {
    var row = $('#grid_zksq').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否提交此转库单？', function (r) {
            if (r) {
                $.ajax({
                    url:$.webPath() + "/kcgl/zksq/tj_zksq",
                    dataType:'json',
                    type:'POST',
                    data:{zkdId:row.id},
                    beforeSend:function () {
                        $("#layout_zksq").mask();
                    },
                    success:function (data) {
                        $("#layout_zksq").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_zksq').datagrid('reload');
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else {
                            $('#grid_zksq').datagrid('reload');
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要审核的领料单！", "warning");
    }
}
//审批方法
function zksq_sh() {
    var row = $('#grid_zksq').datagrid('getSelected');
    if (row) {
        $.messager.confirm('提示', '是否审批此转库单？', function (r) {
            if (r) {
                $.ajax({
                    url:$.webPath() + "/kcgl/zksq/sh_zksq",
                    dataType:'json',
                    type:'POST',
                    data:{zkdId:row.id},
                    beforeSend:function () {
                        $("#layout_zksq").mask();
                    },
                    success:function (data) {
                        $("#layout_zksq").mask("hide");
                        if (data.statusCode == 200) {
                            $('#grid_zksq').datagrid('reload');
                            $.messager.show({
                                title:'提示',
                                msg:data.message,
                                timeout:3000,
                                showType:'slide'
                            });
                        } else {
                            $('#grid_zksq').datagrid('reload');
                            $.messager.alert("提示", data.message, "error");
                        }
                    }
                });
            }
        });
    } else {
        $.messager.alert("提示", "请选择要审核的领料单！", "warning");
    }
}
//查询方法
function zksq_search() {
    $('#grid_zksq').datagrid('load',
        {
            zkdh:$('#zksq_zkdh').val()
        }
    );
}
